﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kangaroo.Controls.Properties;
using Kangaroo.Model;

namespace Kangaroo.Controls
{
    public partial class MapMarker : UserControl
    {
        public ChannelInfo Channel { get; private set; }
        private Size _parentSize;
        private Point _position;
        private const int MarkerRadius = 15;

        public MapMarker(ChannelInfo channel, Size parentSize, Point position)
        {
            Channel = channel;
            _parentSize = parentSize;
            _position = position;
            this.Hide();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();
            this.BackColor = Color.Transparent;
            name.Text = string.Format("{0} {1}", channel.EquipId, channel.EquipPhase);
            icon.Width = icon.Height = MarkerRadius;
            SetPosition(position);
            this.Show();
        }

        private void MapMarker_Load(object sender, EventArgs e)
        {
            DrawIcon();
        }

        private int _alarmLevel;
        public int AlarmLevel
        {
            get { return _alarmLevel; }
            set
            {
                if(value != _alarmLevel)
                {
                    _alarmLevel = value;
                    DrawIcon();
                }
            }
        }

        private void SetPosition(Point position)
        {
            var iconLocation = LocationInControl(icon);
            this.Top = position.Y - iconLocation.Y - icon.Height / 2;
            this.Left = position.X - iconLocation.X - icon.Width / 2;
        }

        private void DrawIcon()
        {
            icon.Image = GetIcon();
        }

        private Image GetIcon()
        {
            switch(_alarmLevel)
            {
                case 0:
                    return Resources.Green;
                case 1:
                case 2:
                case 3:
                case 4:
                    return Resources.Yellow;
                case 5:
                    return Resources.Red;
                default:
                    throw new InvalidOperationException(string.Format("Unknown alarm level {0}", _alarmLevel));
            }
        }

        private Point LocationInControl(Control control)
        {
            return PointToClient(control.PointToScreen(Point.Empty));
        }

        public void UpdateParentSize(Size clientSize)
        {
            var ratioX = clientSize.Width * 1.0 / _parentSize.Width;
            _position.X = (int)Math.Round(_position.X * ratioX);
            var ratioY = clientSize.Height * 1.0 / _parentSize.Height;
            this._position.Y = (int)Math.Round(_position.Y * ratioY);
            SetPosition(_position);
            _parentSize = clientSize;
        }

        private void icon_Click(object sender, EventArgs e)
        {
            OnIconClick();
        }

        public event EventHandler IconClick;

        protected void OnIconClick()
        {
            var temp = IconClick;
            if(temp != null)
            {
                temp(this, EventArgs.Empty);
            }
        }
    }
}
